import { useState, useCallback, useEffect } from "react";

interface UseCounterResult {
  count: number;
  increment: () => void;
  decrement: () => void;
  reset: () => void;
}

function useCounter(initialValue: number = 0): UseCounterResult {
  const [count, setCount] = useState(initialValue);

  const increment = useCallback(() => setCount((prev) => prev + 1), []);
  const decrement = useCallback(() => setCount((prev) => prev - 1), []);
  const reset = useCallback(() => setCount(initialValue), [initialValue]);

  // 更新文件标题。
  useEffect(() => {
    document.title = `当前计数: ${count}`;
  }, [count]);

  return { count, increment, decrement, reset };
}

export default useCounter;
